package view;

import controller.Commandable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * Class to represent a Console used for entering input and displaying error messages. A console is a JPanel with
 * action and key listeners to respond to user input.
 * @author Niel Lebeck and Ben Schwab
 *
 */
public class Console extends JPanel implements ActionListener, KeyListener {
    private static final int NUMBER_OF_ROWS = 4;
    private static JTextArea ourConsoleText;
    private JTextField myConsoleInput;
    private JScrollPane myConsoleTextWrapper;
    private ArrayList<String> myCommandHistory = new ArrayList<String>();
    private int myHistoryPos = 0;
    private Commandable<?, ?> myCommandTarget;

    /**
     * Console constructor. Creates a 
     * @param commandTarget a Commandable object that the view can outsource user actions to.
     */
    public Console (Commandable<?, ?> commandTarget) {
        myCommandTarget = commandTarget;
        setLayout(new BorderLayout());
        ourConsoleText = new JTextArea();
        ourConsoleText.setRows(NUMBER_OF_ROWS);
        ourConsoleText.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        ourConsoleText.setEditable(false);
        myConsoleTextWrapper = new JScrollPane(ourConsoleText);
        myConsoleInput = new JTextField();
        myConsoleInput.addKeyListener(this);
        myConsoleInput.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add(myConsoleTextWrapper, BorderLayout.PAGE_START);
        add(myConsoleInput, BorderLayout.PAGE_END);
        // put in dummy text
        ourConsoleText.setText("Welcome to Web Visualization 308!");
        myConsoleInput.setText("type commands here");
        myConsoleInput.addActionListener(this);

    }

    @Override
    public void actionPerformed (ActionEvent arg0) {
        String command = myConsoleInput.getText();
        appendConsoleText(command);
        myCommandHistory.add(command);
        myCommandTarget.processCommand(command.toLowerCase());
        myConsoleInput.setText(null);
    }

    /**
     * Append text to the history console.
     * 
     * @param command String to append to the console
     */
    public static void appendConsoleText (String command) {
        String newConsoleText = ourConsoleText.getText() + "\n" + command;
        ourConsoleText.setText(newConsoleText);
    }

    @Override
    public void keyPressed (KeyEvent arg0) {
        if (myCommandHistory.size() > 0) {
            switch (arg0.getKeyCode()) {
                case KeyEvent.VK_UP:
                    myHistoryPos = Math.abs(myHistoryPos - 1) %
                            myCommandHistory.size();

                    break;
                case KeyEvent.VK_DOWN:
                    myHistoryPos = (myHistoryPos + 1) % myCommandHistory.size();
                    break;
                default:

                    return;
            }
            String prompt = myCommandHistory.get(myHistoryPos);
            myConsoleInput.setText(prompt);

        }
    }

    @Override
    public void keyReleased (KeyEvent arg0) {

    }

    @Override
    public void keyTyped (KeyEvent arg0) {

    }

}
